Test Failed
Push — develop ( 6e6be8...a91838 )
by Freddie
13:32
created

main.js ➔ getCookie   F

Complexity

Conditions 37

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 37
eloc 10
dl 0
loc 19
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like main.js ➔ getCookie often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
jQuery(document).ready(function ($) {
2
    'use strict';
3
    if ($('.notification-list').length) {
4
        $('.notification-list').slimScroll({
5
            height: '100%'
6
        });
7
    }
8
    if ($('.menu-list').length) {
9
        $('.menu-list').slimScroll({
10
            height: '100%'
11
        });
12
    }
13
    if ($('.navbar-nav > .nav-item > a').length) {
14
        $('.navbar-nav > .nav-item > a').click(function () {
15
            if ($(this).hasClass('active')) {
16
                $(this).removeClass('active');
17
                $(this).attr('aria-expanded', 'false');
18
19
                return;
20
            }
21
22
            $('.navbar-nav > .nav-item > a').each(function () {
23
                $(this).removeClass('active');
24
                $(this).attr('aria-expanded', 'false');
25
26
                const target = $(this).data('target');
27
28
                $(target).removeClass('show');
29
                $(target).attr('aria-expanded', 'false');
30
            })
31
32
            $(this).addClass('active');
33
            $(this).attr('aria-expanded', 'true');
34
        });
35
    }
36
    if ($('.sidebar-nav-fixed a').length) {
37
        $('.sidebar-nav-fixed a').click(function (event) {
38
            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
39
                && location.hostname == this.hostname
40
            ) {
41
                var target = $(this.hash);
42
                target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
43
                if (target.length) {
44
                    event.preventDefault();
45
                    $('html, body').animate({
46
                        scrollTop: target.offset().top - 90
47
                    }, 1000, function () {
48
                        var $target = $(target);
49
                        $target.focus();
50
                        if ($target.is(':focus')) {
51
                            return false;
52
                        }
53
54
                        $target.attr('tabindex', '-1');
55
                        $target.focus();
56
                        return true;
57
                    });
58
                }
59
            };
60
61
            $('.sidebar-nav-fixed a').each(function () {
62
                $(this).removeClass('active');
63
            })
64
65
            $(this).addClass('active');
66
        });
67
    }
68
69
    if ($('[data-toggle="tooltip"]').length) {
70
        $('[data-toggle="tooltip"]').tooltip()
71
    }
72
73
    if ($('[data-toggle="popover"]').length) {
74
        $('[data-toggle="popover"]').popover()
75
    }
76
77
    $('.money-format').each(function () {
78
        $(this).html(getMoneyFormat($(this).html()));
79
    });
80
81
    $(document).on('submit', 'form[data-confirmation]', function (event) {
82
        var $form = $(this),
83
            $confirm = $('#confirmationModal');
84
85
        if ($confirm.data('result') !== 'yes') {
86
            event.preventDefault();
87
88
            $confirm
89
                .off('click', '#btnYes')
90
                .on('click', '#btnYes', function () {
91
                    $confirm.data('result', 'yes');
92
                    $form.find('input[type="submit"]').attr('disabled', 'disabled');
93
                    $form.submit();
94
                })
95
                .modal('show');
96
        }
97
    }).on('submit', 'form:not([data-confirmation])', function () {
98
        $('.overlay').show();
99
    }).on('click', '.show-overlay', function () {
100
        $('.overlay').show();
101
    }).on('change', '.money-format', function () {
102
        const $html = $(this);
103
        const isInput = $html.is('input');
104
        const number = isInput ? $html.val() : $html.html();
105
        const money = getMoneyFormat(number);
106
107
        $html.data('mf-amount', number);
108
109
        isInput ? $html.val(money) : $html.html(money);
110
    });
111
112
    /** global: InfiniteScroll */
113
    if (typeof InfiniteScroll === 'function') {
0 ignored issues
show
Bug introduced by
The variable InfiniteScroll seems to be never declared. If this is a global, consider adding a /** global: InfiniteScroll */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114
        const infScroll = new InfiniteScroll('.dashboard-content', {
115
            path: function () {
116
                /** global: URLSearchParams */
117
                const page= (parseInt((new URLSearchParams(window.location.search)).get('page') || this.pageIndex) + 1);
0 ignored issues
show
Bug introduced by
The variable URLSearchParams seems to be never declared. If this is a global, consider adding a /** global: URLSearchParams */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
118
                const form = document.querySelector('.dashboard-sidebar form');
119
                let queryFilters = '';
120
121
                if (form) {
122
                    /** global: FormData */
123
                    queryFilters = '&' + new URLSearchParams(new FormData(form)).toString();
124
                }
125
126
                return '?page=' + page + queryFilters;
127
            },
128
            responseType: 'html',
129
            status: '.infinite-scroll-status',
130
            history: false,
131
        }).on('load', function (html) {
132
            if (html === '') {
133
                infScroll.dispatchEvent('last');
134
135
                return;
136
            }
137
138
            document.querySelector('.dashboard-content .table > tbody').innerHTML += html;
139
140
            const moneyFormats = document.querySelectorAll('.dashboard-content .table > tbody > tr > td.money-format');
141
142
            [].forEach.call(moneyFormats, function (moneyFormat) {
143
                if (isNaN(moneyFormat.innerHTML)) {
144
                    return undefined;
145
                }
146
147
                moneyFormat.innerHTML = getMoneyFormat(moneyFormat.innerHTML);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
148
            });
149
        });
150
151
        window.infScroll = infScroll;
152
    }
153
});
154
155
function getCookie(cname)
156
{
157
    const name = cname + '=';
158
    const ca = document.cookie.split(';');
159
160
    for (let i = 0; i < ca.length; i++) {
161
        let c = ca[i];
162
163
        while (c.charAt(0) == ' ') {
164
            c = c.substring(1);
165
        }
166
167
        if (c.indexOf(name) === 0) {
168
            return c.substring(name.length, c.length);
169
        }
170
    }
171
172
    return '';
173
}
174
175
function getMoneyFormat(number)
176
{
177
    if (isNaN(number)) {
178
        return 0;
179
    }
180
181
    /** global: Intl */
182
    if (!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function')) {
183
        return number;
184
    }
185
186
    return (number * 1).toLocaleString('es-CO', {
187
        style: 'currency',
188
        currency: 'COP',
189
        minimumFractionDigits: 0,
190
        maximumFractionDigits: 2
191
    });
192
}
193